package com.iot08.api.service;

import com.aliyun.tea.utils.StringUtils;
import com.iot08.api.response.GoldCoinResp;
import com.iot08.api.utils.StringTool;
import com.iot08.api.vo.request.*;
import com.iot08.api.vo.request.base.BodyFormAccount;
import com.iot08.common.entity.app.GoldCoinDesc;
import com.iot08.common.entity.app.GoldCoinProductInfo;
import com.iot08.common.entity.app.ProductInfo;
import com.iot08.common.entity.app.VipMember;
import com.iot08.common.service.base.BaseAdminService;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import com.iot08.common.utils.StringUtil;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * @author cwj
 * @description
 * @date 2025/3/6 9:41
 **/
@Slf4j
@Service
public class GoldCoinService extends BaseAdminService<GoldCoinProductInfo> {

    @Resource
    private WallpaperService wallpaperService;
    
    @Resource
    private UserSmsNotifyService userSmsNotifyService;

    @Resource
    private GoldCoinDescService goldCoinDescService;

    @Resource
    private VipMemberService vipMemberService;

    @Resource
    private ProductInfoService productInfoService;

    /**
     * @description 查询所有有效的金币产品信息
     * @author cwj
     * @param []
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/6 9:54
     **/
    public ResponseMap queryGoldCoin(VersionVo versionVo,String openId) {
        if(ObjectUtils.isEmpty(versionVo.getUserAgent())) {
            return ResponseMap.fail("参数不正确");
        }
        ResponseMap res = ResponseMap.success();
        Query query = new Query();
        boolean isAndroid = versionVo.getUserAgent().contains("Android");
        if(isAndroid) {
            //查询正在使用中安卓会员产品信息
            query.addCriteria(Criteria.where("sysType").is(1).and("isCurrentUsing").is(1));
        } else {
            query.addCriteria(Criteria.where("sysType").is(2).and("isCurrentUsing").is(1));
        }
        List<GoldCoinResp> goldCoinResps = mongoTemplate.find(query,GoldCoinResp.class,"gold_coin");
        //会员享受会员折扣，非会员享受非会员折扣
        VipMember vipMember = vipMemberService.queryVipMemberByOpenid(openId);
        if(ObjectUtils.isNotEmpty(vipMember) && !StringUtils.isEmpty(vipMember.getPId())) {
            //查询会员产品的折扣信息
            ProductInfo productInfo = productInfoService.findById(new ObjectId(vipMember.getPId()));
            if(ObjectUtils.isNotEmpty(productInfo) && ObjectUtils.isNotEmpty(productInfo.getPayGoldCoinDiscount())) {
                goldCoinResps.forEach(g->{
                    g.setDiscount(productInfo.getPayGoldCoinDiscount());
                    g.setDiscountDesc(productInfo.getGoldCoinDiscountDesc());
                });
            }
        }
        GoldCoinDesc goldCoinDesc = goldCoinDescService.queryGoldCoinDesc();
        if(!CollectionUtils.isEmpty(goldCoinResps)) {
            res.put("goldCoinResp",goldCoinResps);
        }
        if(ObjectUtils.isNotEmpty(goldCoinDesc) && !StringUtils.isEmpty(goldCoinDesc.getGoldCoinDescContent())) {
            res.put("goldCoinDesc",goldCoinDesc.getGoldCoinDescContent());
        }
        return res;
    }

    /**
     * @description 金币购买服务
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/20 11:58
     **/
    public ResponseMap buyServer(GoldCoinBuyServerReqBodyFormVo vo){
        if(Integer.valueOf(1).equals(vo.getType())) {
            WallpaperBuyerReqBodyFormVo bodyForm = new WallpaperBuyerReqBodyFormVo();
            bodyForm.setWid(vo.getWid());
            bodyForm.setSign(vo.getSign());
            bodyForm.setOpenid(vo.getOpenid());
            bodyForm.setAccesstoken(vo.getAccesstoken());
            bodyForm.setTimeStamp(vo.getTimeStamp());
            return wallpaperService.buyWallpaper(bodyForm);
        } else if(Integer.valueOf(2).equals(vo.getType())){
            userSmsNotifyService.buyUserSmsNotify(transferGoldCoinBuyServerReqVoToUserSmsNotifyBuyReqBodyForm(vo));
        }
        return ResponseMap.success();
    }

    /**
     * @description 数据对象转换
     * @author cwj
     * @param [vo]
     * @return com.iot08.api.vo.request.UserSmsNotifyBuyReqBodyFormVo
     * @date @time 2025/3/20 15:06
     **/
    private UserSmsNotifyBuyReqBodyFormVo transferGoldCoinBuyServerReqVoToUserSmsNotifyBuyReqBodyForm(GoldCoinBuyServerReqBodyFormVo vo) {
        UserSmsNotifyBuyReqBodyFormVo bodyForm = new UserSmsNotifyBuyReqBodyFormVo();
        bodyForm.setSnId(vo.getSId());
        bodyForm.setOpenid(vo.getOpenid());
        bodyForm.setSmsNotifyTime(vo.getServerTime());
        bodyForm.setSign(vo.getSign());
        bodyForm.setAccesstoken(vo.getAccesstoken());
        bodyForm.setTimeStamp(vo.getTimeStamp());
        return bodyForm;
    }

    /**
     * @description 新增金币产品信息
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/7 11:07
     **/
    public ResponseMap addGoldCoin(GoldCoinAddReqVo vo) {
        GoldCoinProductInfo goldCoinProductInfo = new GoldCoinProductInfo();
        goldCoinProductInfo.setName(vo.getName());
        goldCoinProductInfo.setSysType(vo.getSysType());
        goldCoinProductInfo.setApplePayProdId(vo.getApplePayProdId());
        goldCoinProductInfo.setType(vo.getType());
        goldCoinProductInfo.setIsRecommend(vo.getIsRecommend());
        goldCoinProductInfo.setPrice(vo.getPrice());
        goldCoinProductInfo.setPriceDesc(vo.getPriceDesc());
        goldCoinProductInfo.setGoldCoinNum(vo.getGoldCoinNum());
        goldCoinProductInfo.setGoldCoinNumDesc(vo.getGoldCoinNumDesc());
        goldCoinProductInfo.setCurrency(vo.getCurrency());
        goldCoinProductInfo.setMonetaryUnit(vo.getMonetaryUnit());
        goldCoinProductInfo.setDiscount(vo.getDiscount());
        goldCoinProductInfo.setDiscountDesc(vo.getDiscountDesc());
        if(!StringUtils.isEmpty(vo.getDiscountStartTime())){
            goldCoinProductInfo.setDiscountStartTime(DateUtil.strToTimestamp(vo.getDiscountStartTime(),DateUtil.FORMAT_1)/1000);
        }
        if(!StringUtils.isEmpty(vo.getDiscountEndTime())) {
            goldCoinProductInfo.setDiscountEndTime(DateUtil.strToTimestamp(vo.getDiscountEndTime(),DateUtil.FORMAT_1)/1000);
        }
        goldCoinProductInfo.setIsCurrentUsing(vo.getIsCurrentUsing());
        goldCoinProductInfo.setDesc(vo.getDesc());
        goldCoinProductInfo.setCreateTime(new Date());
        goldCoinProductInfo.setUpdateTime(new Date());
        this.save(goldCoinProductInfo);
        return ResponseMap.success("新增成功");
    }

    /**
     * @description 修改金币的产品信息
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/7 11:07
     **/
    public ResponseMap editGoldCoin(GoldCoinEditReqVo vo) {
        Update update = new Update();
        if(!StringUtils.isEmpty(vo.getName())){
            update.set("name",vo.getName());
        }
        if(ObjectUtils.isNotEmpty(vo.getSysType())){
            update.set("sysType",vo.getSysType());
        }
        if(!StringUtils.isEmpty(vo.getApplePayProdId())){
            update.set("applePayProdId",vo.getApplePayProdId());
        }
        if(!ObjectUtils.isEmpty(vo.getType())){
            update.set("type",vo.getType());
        }
        if(!ObjectUtils.isEmpty(vo.getIsRecommend())){
            update.set("isRecommend",vo.getIsRecommend());
        }
        if(!ObjectUtils.isEmpty(vo.getPrice())){
            update.set("price",vo.getPrice());
        }
        if(!StringUtils.isEmpty(vo.getPriceDesc())){
            update.set("priceDesc",vo.getPriceDesc());
        }
        if(!ObjectUtils.isEmpty(vo.getGoldCoinNum())){
            update.set("goldCoinNum",vo.getGoldCoinNum());
        }
        if(!StringUtils.isEmpty(vo.getGoldCoinNumDesc())){
            update.set("goldCoinNumDesc",vo.getGoldCoinNumDesc());
        }
        if(!StringUtils.isEmpty(vo.getCurrency())){
            update.set("currency",vo.getCurrency());
        }
        if(!StringUtils.isEmpty(vo.getMonetaryUnit())){
            update.set("monetaryUnit",vo.getMonetaryUnit());
        }
        if(!ObjectUtils.isEmpty(vo.getDiscount())){
            update.set("discount",vo.getDiscount());
        }
        if(!StringUtils.isEmpty(vo.getDiscountDesc())) {
            update.set("discountDesc",vo.getDiscountDesc());
        }
        if(!StringUtils.isEmpty(vo.getDiscountStartTime())){
            update.set("discountStartTime",DateUtil.strToTimestamp(vo.getDiscountStartTime(),DateUtil.FORMAT_1)/1000);
        }
        if(!StringUtils.isEmpty(vo.getDiscountEndTime())) {
            update.set("discountEndTime",DateUtil.strToTimestamp(vo.getDiscountEndTime(),DateUtil.FORMAT_1)/1000);
        }
        if(ObjectUtils.isNotEmpty(vo.getIsCurrentUsing())) {
            update.set("isCurrentUsing",vo.getIsCurrentUsing());
        }
        if(!StringUtils.isEmpty(vo.getDesc())){
            update.set("desc",vo.getDesc());
        }
        update.set("updateTime",new Date());
        this.updateById(new ObjectId(vo.getGid()),update);
        return ResponseMap.success("更新成功");
    }
}
